package jkCore.social.ad.adapter;

import android.app.Activity;
import android.view.KeyEvent;
import android.view.ViewGroup;

import jkCore.social.ad.AdManager;
import jkCore.social.ad.config.AdConfigValue;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

public class AdBaseAdapter {

    protected static final String TOP_TAG = "AdManager";

    protected Activity mActivity;
    public AdConfigValue mConfigValue;

    // callback
    protected AdManager.AdCallBack adBannerCallBack;
    protected AdManager.AdCallBack adInterCallBack;
    protected AdManager.AdCallBack adVideoCallBack;
    protected AdManager.AdCallBack adNativeCallBack;
    protected AdManager.AdCallBack adFloatCallBack;
    protected AdManager.AdCallBack adBoxCallBack;
    protected AdManager.AdCallBack adSplashCallBack;
    // 准备状态
    protected boolean isBannerReady = false;
    protected boolean isInterReady = false;
    protected boolean isVideoReady = false;
    protected boolean isNativeReady = false;
    protected boolean isFloatReady = false;
    protected boolean isBoxReady = false;
    protected boolean isSplashReady = false;
    // 加载状态
    protected boolean isBannerLoading = false;
    protected boolean isInterLoading = false;
    protected boolean isVideoLoading = false;
    protected boolean isNativeLoading = false;
    protected boolean isFloatLoading = false;
    protected boolean isBoxLoading = false;
    protected boolean isSplashLoading = false;
    // 状态信息
    protected boolean isTryShowBanner = false;
    protected boolean isVideoPlayFinish = false;
    protected boolean isInit = false;
    protected boolean isDebug = false;
    protected boolean isRemoveAds = false;

    private Timer mInitTimer;
    private int mTryInitCount;
    private Timer mInterLoadTimer;
    private int mTryInterLoadCount;

    public AdBaseAdapter() {
        mConfigValue = new AdConfigValue();
    }

    // 设置debug模式
    public void setDebug(boolean debug) {
        isDebug = debug;
    }

    // 移除适配器
    public void setRemoveAds(boolean removeAds) {
        isRemoveAds = removeAds;
    }

    // 初始化
    public void tryInitAd() {
        stopTryInitAd();
        if (mActivity == null) {
            return;
        }
        mTryInitCount++;
        mInitTimer = new Timer();
        mInitTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initAd(mActivity);
                    }
                });
            }
        }, (int) Math.pow(2, mTryInitCount) * 1000);
    }

    // 清除初始化timer
    private void stopTryInitAd() {
        if (mInitTimer != null) {
            mInitTimer.cancel();
            mInitTimer = null;
        }
    }

    // 加载插屏广告
    public void tryLoadInterAds() {
        stopTryLoadInterAds();
        if (mTryInterLoadCount >= 10 || mActivity == null) {
            return;
        }
        mTryInterLoadCount++;
        mInterLoadTimer = new Timer();
        mInterLoadTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadInterAds();
                    }
                });
            }
        }, (int) Math.pow(2, mTryInterLoadCount) * 1000);
    }

    private void stopTryLoadInterAds() {
        if (mInterLoadTimer != null) {
            mInterLoadTimer.cancel();
            mInterLoadTimer = null;
        }
    }

    public void finishTryLoadInterAds() {
        mTryInterLoadCount = 0;
        stopTryLoadInterAds();
    }

    // 加载所有广告
    public void loadAllAds() {
        stopTryInitAd();
        isInit = true;
        if (mActivity == null) {
            return;
        }
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadBannerAds();
                loadInterAds();
                loadNativeAds();
                loadRewardAds();
                loadFloatAds();
                loadBoxAds();
                if (mTryInitCount == 0) {
                    loadSplashAds();
                }
            }
        });
    }

    // 横幅广告
    public boolean isBannerReady() {
        if (!isBannerReady) {
            loadBannerAds();
        }
        return isBannerReady;
    }

    public void showBanner(AdManager.AdCallBack callBack) {
    }

    public void hideBanner() {
    }

    // 插屏广告
    public boolean isInterReady() {
        if (!isInterReady) {
            loadInterAds();
        }
        return isInterReady;
    }

    public void showInter(AdManager.AdCallBack callBack) {
    }

    public void hideInter() {
    }

    // 激励广告
    public boolean isVideoReady() {
        if (!isVideoReady) {
            loadRewardAds();
        }
        return isVideoReady;
    }

    public void showVideo(AdManager.AdCallBack callBack) {
    }

    public void hideVideo() {
    }

    // 原生广告
    public boolean isNativeReady() {
        if (!isNativeReady) {
            loadNativeAds();
        }
        return isNativeReady;
    }

    public void showNative(ViewGroup view, AdManager.AdCallBack callBack) {
    }

    public void hideNative() {
    }

    // 悬浮广告
    public boolean isFloatReady() {
        if (!isFloatReady) {
            loadFloatAds();
        }
        return isFloatReady;
    }

    public void showFloat(ViewGroup view, AdManager.AdCallBack callBack) {
    }

    public void hideFloat() {
    }

    // 矩形广告
    public boolean isBoxReady() {
        if (!isBoxReady) {
            loadBoxAds();
        }
        return isBoxReady;
    }

    public void showBox(ViewGroup view, AdManager.AdCallBack callBack) {
    }

    public void hideBox() {
    }

    // 开屏广告
    public boolean isSplashReady() {
        if (!isSplashReady) {
            loadSplashAds();
        }
        return isSplashReady;
    }

    public void showSplash(AdManager.AdCallBack callBack) {
    }

    public void hideSplash() {
    }

    public void initAd(Activity context) {
        mActivity = context;
    }

    // 以下做方法声明 子类实现
    public String adName() {
        return "Base";
    }

    public boolean isOut() {
        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        return false;
    }

    public void onResume() {
    }

    public void onPause() {
    }

    public void onDestroy() {
    }

    public void loadBannerAds() {
    }

    public void loadInterAds() {
    }

    public void loadRewardAds() {
    }

    public void loadNativeAds() {
    }

    public void loadFloatAds() {
    }

    public void loadBoxAds() {
    }

    public void loadSplashAds() {
    }
}
